using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System;

namespace sqxt
{
    class AutoNumber
    {
        //private static string atnStr = "440113009";
        private static string msStr;
        private static int ic;
        private static int atnLen ;
        private static string MedSite_name;
        public static string updStr;

        /*=======================================================================================
         * <p>Get an autoNumber with a given MedSite</p>
         * @ return String
         * @ param string
         ========================================================================================*/
        public static string getAutoNumber(String medSite_name)
        {
           string atnStr = "440113009";
           string seltStr = "select MedSite_code," +
                             "Inhabitant_code," +
                             "Lenght " +
                             "from autonumber " +
                             "where MedSite_name = '" + medSite_name + "'";
           MedSite_name = medSite_name;
            SqlConnection connt = new SqlConnection(Dbconnect.s1);
            SqlDataReader dr;
            try
            {
                SqlCommand cmd = new SqlCommand(seltStr, connt);
                connt.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    msStr = dr["MedSite_code"].ToString();
                    //ic = dr["Inhabitant_code"];
                    ic = dr.GetInt16(1);
                    //atnLen = (int)dr["Lenght"];
                    atnLen = dr.GetInt16(2);

                    string icStr = ic + "";
                    int n1 = icStr.Length;
                    int zeroNum = atnLen - n1;
                    for (int i = 0; i < zeroNum; i++)
                    {
                        icStr = "0" + icStr;
                    }
                    atnStr = atnStr + msStr + icStr;
                    ic++;
                    int n2 = icStr.Length;
                    if (n2 > atnLen)
                    {
                        return null;
                    }
                    //connt.Close();
                    updata();
                    return atnStr;
                }

                return null;
            }
            catch (SqlException se)
            {
                return null;
            }
        }

        private static int updata()
        {
            updStr = "update autonumber " +
                                "set MedSite_code = '" + msStr + "'," +
                                "Inhabitant_code = '" + ic + "'," +
                                "Lenght = '" + atnLen + "' "+
                                "where MedSite_name = '" + MedSite_name + "'";
            try
            {
                SqlConnection connt = new SqlConnection(Dbconnect.s1);
                connt.Open();
                SqlCommand cmd = new SqlCommand(updStr, connt);
                
                int n = cmd.ExecuteNonQuery();
                return n;
                connt.Close();
            }
            catch (SqlException se)
            {
                return 0;
            }
        }
    }
}
